package com.example.controller;

import com.example.entity.Users;
import com.example.service.impl.UsersServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;




@Controller
@RequestMapping("/admin")
public class UsersCtrl {
    @Resource
    UsersServiceImpl usersService;

    @GetMapping("selectOne")
    public Users selectOne(Integer id) {return this.usersService.queryById(id);}

    @RequestMapping(value = "/users",method = {RequestMethod.POST,RequestMethod.GET})
    public String findAllUsers(Model model, Users users,
                               @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",required = false, defaultValue = "4") Integer pageSize){
        PageInfo<Users> usersListPage =  usersService.queryAll(users,pageNum,pageSize);

        model.addAttribute("usersListPage",usersListPage);
        return "usersList";
    }

    @GetMapping("/delUsers/{id}")
    public String delUsers(@PathVariable("id") int id, Model model) {
        usersService.deleteById(id);

        model.addAttribute("msg", "删除成功");
        return "forward:/admin/users";
    }

    @GetMapping("/users/{id}")
    public String getUsersById(@PathVariable("id") int id, Model model) {
        Users users = usersService.queryById(id);
        model.addAttribute("users", users);
        return "uesrsEdit";
    }
    @PostMapping("/editUsers")
    public String updateUsers(Users users,Model model){

        System.out.println(users);
        Users i =usersService.update(users);
        model.addAttribute("msg","编辑成功！");
        return "forward:/admin/users";
    }

    @GetMapping("/gotoAddUsers")
    public String gotoAddusers(){

        return "usersAdd";
    }

    @PostMapping("/addUsers")
    public String addUsers(Users users){

        Users i= usersService.insert(users);
        return "forward:/admin/users";
    }
}
